<template>
  <!-- 充值卡明细 -->
  <div class="app-page">
    <div class="top-info">
      <div class="top-info-box">
        <el-card shadow="never">
          <img :src="image">
          <div style="line-height: 40px;">
            <p>累计充值会员</p>
            <div class="text">{{ infoData.totalCsmNum }}<span>人</span></div>
          </div>
        </el-card>
      </div>
      <div class="top-info-box">
        <el-card shadow="never">
          <img :src="price">
          <div style="line-height: 40px;">
            <p>累计充值金额(含赠送)</p>
            <div class="text">{{ numberFormat1(infoData.totalCardAndSendMoney) }}<span>元</span></div>
          </div>
        </el-card>
      </div>
      <div class="top-info-box">
        <el-card shadow="never">
          <img :src="price">
          <div style="line-height: 40px;">
            <p>累计消费金额</p>
            <div class="text">{{ numberFormat1(infoData.totalPayMoney) }}<span>元</span></div>
          </div>
        </el-card>
      </div>
      <div class="top-info-box">
        <el-card shadow="never">
          <img :src="price">
          <div style="line-height: 40px;">
            <p>剩余充值金额(含赠送)</p>
            <div class="text">{{ numberFormat1(infoData.cashAndSendMoney) }}<span>元</span></div>
          </div>
        </el-card>
      </div>
    </div>
    <div class="table">
      <el-table-form ref="form">
        <el-form-item class="merge-item">
          <el-select v-model="qtype" style="width: 180px; margin-right: 2px;">
            <el-option label="客户编号" value="csmCode" />
            <el-option label="客户名称" value="csmName" />
            <el-option label="充值卡号" value="cardCode" />
            <el-option label="电话" value="csmPhone" />
          </el-select>
          <el-input v-model="qtTxt" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="充值机构:">
          <el-tree-select
            v-model="query.orgSNNo"
            :clearable="false"
            class="tree-select"
            :popover-width="360"
            :select-params="{
              clearable: false,
              placeholder: '请选择责任机构',
            }"
            :tree-params=" {
              'default-expand-all': true,
              'expand-on-click-node': false,
              'highlight-current': true,
              props:{
                value:'orgSNNo',
                label: 'orgName',
                disabled: 'disabled',
              },
              data: treeData
            }"
          />
        </el-form-item>
        <el-form-item label="开卡日期:" :table-form-merge="2">
          <el-date-picker
            v-model="date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item label="卡状态:">
          <el-select v-model="query.cardStatus">
            <el-option label="激活" :value="2" />
            <el-option label="注销" :value="-1" />
            <el-option label="冻结" :value="3" />
          </el-select>
        </el-form-item>
        <template v-slot:btns>
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        </template>
      </el-table-form>
      <lb-table
        ref="lbTable"
        v-loading="$loadingPlugin.fetchData"
        v-el-height-adaptive-table="{bottomOffset:10}"
        border
        highlight-current-row
        element-loading-text="加载中"
        header-row-class-name="table-row-name"
        :column="column"
        :data="tableData.data"
        pagination
        :total="tableData.total"
        custom-column
        @fetch="fetchData"
      />
    </div>
    <crad-info ref="cradInfo" :deal-map="dealMap" />
  </div>
</template>

<script>
  import { parseTime } from '@/utils'
  import { topUpAdministrationInfoList, topUpAdministrationInfoInfo } from '@/api'
  import moment from 'dayjs'
  import CradInfo from './CradInfo.vue'

  export default {
    name: 'refil-card-detail',
    components: {
      CradInfo
    },
    props: {
      treeData: {
        type: Array,
        required: true
      },
      dealMap: {
        type: Array,
        required: true
      },
      infoData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        image: require('../../../../assets/person.jpg'),
        price: require('../../../../assets/price.jpg'),
        query: {
          cardStatus: '',
          orgSNNo: ''
        },
        qtTxt: '',
        qtype: 'csmCode',
        date: [],
        tableData: {
          total: 0,
          data: []
        }
      }
    },
    computed: {
      column: function() {
        return [
          {
            prop: 'cardCode',
            label: '充值卡号',
            width: 150
          },
          {
            prop: 'cardStatus',
            label: '充值卡状态',
            width: 150,
            render: (h, { row }) => <span>{row.cardStatus && this.filtersType(row.cardStatus) }</span>
          },

          {
            prop: 'csmCode',
            label: '绑定客户编号',
            width: 150
          },
          {
            prop: 'csmName',
            label: '绑定客户名称',
            width: 150
          },
          {
            prop: 'csmPhone',
            label: '绑定客户电话',
            width: 150
          },
          {
            prop: 'createTime',
            label: '开卡时间',
            width: 100,
            render: (h, { row }) => <span>{ row.createTime && parseTime(row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }</span>
          },
          {
            prop: 'cardSurplus',
            label: '卡余额（元）',
            width: 150,
            render: (h, { row }) => <span>{row.cardSurplus && this.numberFormat(row.cardSurplus, row)}</span>
          },
          {
            prop: 'createUser',
            label: '开卡人',
            width: 100
          },
          {
            prop: 'orgName',
            label: '开卡机构',
            width: 150
          },
          {
            prop: 'refundRemark',
            label: '备注',
            width: 150,
            render: (h, { row }) => <span>{row.refundRemark && this.chooseFormat(row.refundRemark, row)}</span>
          },
          {
            label: '操作',
            align: 'center',
            fixed: 'right',
            render: (h, { row, $index }) => {
              return (
                <div>
                  {(row.cardStatus === -1 || row.cardStatus === 2) && <el-link size='mini' onClick={() => this.csmcardDetail(row, $index)}>充值卡详情</el-link>}
                </div>
              )
            }
          }
        ]
      }
    },
    async created() {},
    methods: {
      async fetchData({ currentPage, pageSize }) {
        const params = {
          ...this._getQuery(this.query),
          pageIndex: currentPage,
          pageSize
        }
        params[this.qtype] = this.qtTxt
        if (this.date && this.date.length === 2) {
          params.startTime = moment(this.date[0]).format()
          params.endTime = moment(this.date[1]).format()
        } else {
          params.startTime = null
          params.endTime = null
        }
        const res = await topUpAdministrationInfoList(params)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      // 充值卡详情
      async csmcardDetail(row) {
        const { data } = await topUpAdministrationInfoInfo({ cardCode: row.cardCode })
        await this.$refs.cradInfo.open(data)
      },
      async upDataCheckTable() {
        await this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      // 清除对象空属性
      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      // 查询
      async search() {
        await this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      // 充值卡状态
      filtersType(v) {
        if (v === -1) {
          return '注销'
        } else if (v === 1) {
          return '待审核'
        } else if (v === 2) {
          return '激活'
        } else if (v === 3) {
          return '冻结'
        }
      },
      // 卡余额（元）
      numberFormat(val, r) {
        var rcType = r.rcType // 为1表示余额 为2表示次数
        if (rcType === 1) {
          val = val * 1.00 / 100
        }
        val = Number(val)
        if (rcType === 1) {
          val = isNaN(val) ? '0.00' : val.toFixed(2)
          return val + ' 元'
        } else if (rcType === 2) {
          return val + ' 次'
        } else {
          return ''
        }
      },
      // 选择展示备注信息 如果是退款销卡,当前备注信息是退款销卡备注 如果是冻结卡 当前备注信息是最后一条冻结卡备注信息
      chooseFormat(val, r) {
        var cardStatus = r.cardStatus // 为1表示余额 为2表示次数
        if (cardStatus === -1 && r.refundRemark !== undefined) {
          return r.refundRemark
        } else {
          if (r.freezeRemark !== undefined) {
            return r.freezeRemark
          }
        }
      },
      // 充值金额
      numberFormat1: function(val) {
        val = val * 1.00 / 100
        val = Number(val)
        val = isNaN(val) ? '0.00' : val.toFixed(2)
        return val
      }
    }
  }
</script>

<style lang="scss" scoped>
.top-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  flex-wrap: wrap;
  .top-info-box {
    width: 290px;
    height: 105px;
    /deep/.el-card__body {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 105px;
    }
    img {
      width: 53px;
      height: 53px;
      background: #01999f;
      border-radius: 6px;
    }
    p {
      font-family: PingFang SC, PingFang SC-Bold;
      font-size: 16px;
      font-weight: 700;
      color: #01999f;
    }
    .text {
      display: block;
      font-family: PingFang SC, PingFang SC-Bold;
      font-size: 22px;
      font-weight: 700;
      color: #353c4e;
      text-align: right;
    }
    span {
      font-size: 14px;
    }
  }
}
</style>
